<?php
namespace Admin\Model;
use Think\Model;
class ArticleClassModel extends Model {
	public $table='zjy_article_class';
	public $page; //当前页
	public $perpage; //每页显示
	public $ac_id; //ID
	public $post; //提交的信息 数组
	public $ids; //IDs 主要用于删除
	public $sorts; //IDs 主要用于删除

	//获得分类列表
	public function get_cat_list()
	{
		$Model=M("ArticleClass");
		$arr=array();
		$i=0;
		$list1=$Model->query("select * from `zjy_article_class` where `ac_type`=1 and `ac_parent_id`=0 order by `ac_sort` asc,`ac_id` desc");
		foreach($list1 as $k1=>$v1){
			$arr[$i]['ac_name']=$v1['ac_name'];
			$arr[$i]['ac_id']=$v1['ac_id'];
			$arr[$i]['ac_show']=$v1['ac_show'];
			$arr[$i]['ac_sort']=$v1['ac_sort'];
			//$arr[$i]['ac_top_name']=$Model->where('pc_id='.$v1['pc_top_id'])->getField('pc_name');
			$arr[$i]['ac_parent_name']=$Model->where('ac_id='.$v1['ac_parent_id'])->getField('ac_name');
			$i++;
			$list2=$Model->query("select * from `zjy_article_class` where `ac_type`=1 and `ac_parent_id`=".$v1['ac_id']." order by `ac_sort` asc,`ac_id` desc");
			foreach($list2 as $v2){
				$arr[$i]['ac_name']='--'.$v2['ac_name'];
				$arr[$i]['ac_id']=$v2['ac_id'];
				$arr[$i]['ac_show']=$v2['ac_show'];
				$arr[$i]['ac_sort']=$v2['ac_sort'];
				//$arr[$i]['pc_top_name']=$Model->where('pc_id='.$v2['pc_top_id'])->getField('pc_name');
				$arr[$i]['ac_parent_name']=$Model->where('ac_id='.$v2['ac_parent_id'])->getField('ac_name');
				$i++;
				/*$list3=$Model->query("select * from `zjy_product_class` where `ac_parent_id`=".$v2['ac_id']." order by `ac_sort` desc,`ac_id` desc");
				foreach($list3 as $v3) {
					$arr[$i]['ac_name'] ='----'.$v3['ac_name'];
					$arr[$i]['ac_id']=$v3['ac_id'];
					$arr[$i]['ac_show']=$v3['ac_show'];
					$arr[$i]['ac_sort']=$v3['ac_sort'];
					$arr[$i]['ac_top_name']=$Model->where('ac_id='.$v3['ac_top_id'])->getField('ac_name');
					$arr[$i]['ac_parent_name']=$Model->where('ac_id='.$v3['ac_parent_id'])->getField('ac_name');
					$i++;
					$list4=$Model->query("select * from `zjy_product_class` where `pc_parent_id`=".$v3['pc_id']." order by `pc_sort` desc,`pc_id` desc");
					foreach($list4 as $v4) {
						$arr[$i]['pc_name'] = '------' . $v4['pc_name'];
						$arr[$i]['pc_id'] = $v4['pc_id'];
						$arr[$i]['pc_is_show']=$v4['pc_is_show'];
						$arr[$i]['pc_sort']=$v4['pc_sort'];
						$arr[$i]['pc_top_name']=$Model->where('pc_id='.$v4['pc_top_id'])->getField('pc_name');
						$arr[$i]['pc_parent_name']=$Model->where('pc_id='.$v4['pc_parent_id'])->getField('pc_name');
						$i++;
					}
				}*/
			}
		}
		return $arr;
	}
	/*public function get_cat_list()
	{
		$Model=M("Article_class"); // 实例化对象
		$field[]='ac_sort';
		$field[]='ac_id';
		$field[]='ac_name';
		$where['ac_parent_id'] = array('eq',0);
		$where['ac_type'] = array('eq',1);
		$order['ac_sort']='asc';
		$order['ac_id']='desc';
		$list=$Model->field($field)->where($where)->order($order)->select();
		return $list;
	}*/
	//获取信息
	public function get_ac_info()
	{
		$ac_id=intval($this->ac_id);
		$Model=M("Article_class");
		$info=$Model->where('ac_id='.$ac_id)->find();
		return $info;
	}
	//提交编辑
	public function cat_edit_post()
	{
		$Model=M("Article_class"); // 实例化对象
		$condition['ac_id'] = $this->post['ac_id'];
		$data=$this->post;
		$result=$Model->where($condition)->save($data);
		if($result === false) {
			return false;
		}else{
			return true;
		}
	}
	//
	public function cat_add_post()
	{
		$Model=M("Article_class"); // 实例化对象
		$data=$this->post;
		// 写入
		if($Model->add($data)){
			return true;
		}else{
			return false;
		}
	}
	//删除
	public function delete_cat()
	{
		$ids=$this->ids;
		$Model=M("ArticleClass");
		foreach($ids as $v){
			$condition['ac_id']=$v;
			if(!$Model->where($condition)->delete()){
				return false;
			}
		}
		return true;
	}
	//批量更新排序
	public function update_cat_sort()
	{
		$ids=$this->ids;
		$sorts=$this->sorts;
		$Model=M("ArticleClass");
		foreach($ids as $k=>$v){
			$data['ac_sort']=$sorts[$k];
			$condition['ac_id']=$v;
			$result=$Model->where($condition)->save($data);
			if($result === false) {
				return false;
			}
		}
		return true;
	}

	//系统文章部分
	//获取产品分类列表 全部放在一页 四级分类
	public function get_system_cat_list()
	{
		$Model=M("ArticleClass");
		$arr=array();
		$i=0;
		$list1=$Model->query("select * from `zjy_article_class` where `ac_type`=2 and `ac_parent_id`=0 order by `ac_sort` asc,`ac_id` desc");
		foreach($list1 as $k1=>$v1){
			$arr[$i]['ac_name']=$v1['ac_name'];
			$arr[$i]['ac_id']=$v1['ac_id'];
			$arr[$i]['ac_show']=$v1['ac_show'];
			$arr[$i]['ac_sort']=$v1['ac_sort'];
			//$arr[$i]['ac_top_name']=$Model->where('pc_id='.$v1['pc_top_id'])->getField('pc_name');
			$arr[$i]['ac_parent_name']=$Model->where('ac_id='.$v1['ac_parent_id'])->getField('ac_name');
			$i++;
			$list2=$Model->query("select * from `zjy_article_class` where `ac_type`=2 and `ac_parent_id`=".$v1['ac_id']." order by `ac_sort` asc,`ac_id` desc");
			foreach($list2 as $v2){
				$arr[$i]['ac_name']='--'.$v2['ac_name'];
				$arr[$i]['ac_id']=$v2['ac_id'];
				$arr[$i]['ac_show']=$v2['ac_show'];
				$arr[$i]['ac_sort']=$v2['ac_sort'];
				//$arr[$i]['pc_top_name']=$Model->where('pc_id='.$v2['pc_top_id'])->getField('pc_name');
				$arr[$i]['ac_parent_name']=$Model->where('ac_id='.$v2['ac_parent_id'])->getField('ac_name');
				$i++;
				/*$list3=$Model->query("select * from `zjy_product_class` where `ac_parent_id`=".$v2['ac_id']." order by `ac_sort` desc,`ac_id` desc");
				foreach($list3 as $v3) {
					$arr[$i]['ac_name'] ='----'.$v3['ac_name'];
					$arr[$i]['ac_id']=$v3['ac_id'];
					$arr[$i]['ac_show']=$v3['ac_show'];
					$arr[$i]['ac_sort']=$v3['ac_sort'];
					$arr[$i]['ac_top_name']=$Model->where('ac_id='.$v3['ac_top_id'])->getField('ac_name');
					$arr[$i]['ac_parent_name']=$Model->where('ac_id='.$v3['ac_parent_id'])->getField('ac_name');
					$i++;
					$list4=$Model->query("select * from `zjy_product_class` where `pc_parent_id`=".$v3['pc_id']." order by `pc_sort` desc,`pc_id` desc");
					foreach($list4 as $v4) {
						$arr[$i]['pc_name'] = '------' . $v4['pc_name'];
						$arr[$i]['pc_id'] = $v4['pc_id'];
						$arr[$i]['pc_is_show']=$v4['pc_is_show'];
						$arr[$i]['pc_sort']=$v4['pc_sort'];
						$arr[$i]['pc_top_name']=$Model->where('pc_id='.$v4['pc_top_id'])->getField('pc_name');
						$arr[$i]['pc_parent_name']=$Model->where('pc_id='.$v4['pc_parent_id'])->getField('pc_name');
						$i++;
					}
				}*/
			}
		}
		return $arr;
	}
	//
	public function system_cat_add_post()
	{
		$Model=M("Article_class"); // 实例化对象
		$data=$this->post;
		// 写入
		if($Model->add($data)){
			return true;
		}else{
			return false;
		}
	}

}